package com.xin.ruanjian.omsystem.view;

import android.os.Bundle;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.xin.ruanjian.omsystem.R;
import com.xin.ruanjian.omsystem.activity.OnlineReservationsActivity;
import com.xin.ruanjian.omsystem.adapter.TimeSelectLeftAdapter;
import com.xin.ruanjian.omsystem.adapter.TimeSelectRightAdapter;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by local123 on 2018/3/2.
 */

public class TimeSelectDialog extends DialogFragment {
    @BindView(R.id.rv_time_select_left)
    RecyclerView rvTimeSelectLeft;
    @BindView(R.id.rv_time_select_right)
    RecyclerView rvTimeSelectRight;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    Unbinder unbinder;

    private String openTime, closeTime;
    public TimeSelectLeftAdapter leftAdapter;
    public TimeSelectRightAdapter rightAdapter;

    private List<String> date = Arrays.asList("今天");
    private int select = 0;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        //运行于onCreateView之前
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NO_TITLE, R.style.style_dialog);
        openTime = getArguments().getString("open");
        closeTime = getArguments().getString("close");
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_time_select, container, false);
        unbinder = ButterKnife.bind(this, view);
        initRecyclerView();
        // 设置宽度为屏宽、靠近屏幕底部。
        Window window = getDialog().getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams wlp = window.getAttributes();
        wlp.gravity = Gravity.BOTTOM;
        wlp.width = WindowManager.LayoutParams.MATCH_PARENT;
        wlp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(wlp);
        return view;
    }

    private void initRecyclerView() {
        rvTimeSelectLeft.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        rvTimeSelectRight.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false));
        leftAdapter = new TimeSelectLeftAdapter(getActivity(), date);
        leftAdapter.setLitener(new TimeSelectLeftAdapter.onItemClickLitener() {
            @Override
            public void onItemOnClick(int select) {

            }
        });
        rvTimeSelectLeft.setAdapter(leftAdapter);
        rightAdapter = new TimeSelectRightAdapter(getActivity(), TimeSelectUtils.getTime(openTime, closeTime, 1200));
        rightAdapter.setLitener(new TimeSelectRightAdapter.onItemClickLitener() {
            @Override
            public void onItemOnClick(String time) {
//                if (TextUtils.equals(date.get(select), "今天")) {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                    time =  format.format(System.currentTimeMillis()) + " " + time;
                    ((OnlineReservationsActivity)getActivity()).tvTime.setText(time);
                    getDialog().dismiss();
//                }else {
                    //暂无
//                }
            }
        });
        rvTimeSelectRight.setAdapter(rightAdapter);
    }

    public static TimeSelectDialog newInstance(String openTime, String closeTime) {
        TimeSelectDialog timeSelectDialog = new TimeSelectDialog();
        Bundle data = new Bundle();
        data.putString("open", openTime);
        data.putString("close", closeTime);
        timeSelectDialog.setArguments(data);
        return timeSelectDialog;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick(R.id.tv_confirm)
    public void onViewClicked() {
        getDialog().dismiss();
    }

    private static class TimeSelectUtils {
        /**
         * 根据起始时间返回相应的时间分段
         * @param openTime
         * @param closeTime
         * @param space 时间间隔（s）
         * @return
         */
        public static List<String> getTime(String openTime, String closeTime, int space) {
            List<String> timeData = new ArrayList<>();
            SimpleDateFormat sdf=new SimpleDateFormat("HH:mm");
            try {
                Date start = sdf.parse(openTime);
                Date end = sdf.parse(closeTime);
                Calendar rightNow = Calendar.getInstance();
                rightNow.setTime(start);
                //首先将当前时间段按照space进行划分
                do {
                    //当前所加时间还在关闭时间之前,加space
                    timeData.add(sdf.format(start));
                    rightNow.add(Calendar.SECOND, space);
                    start = rightNow.getTime();
                } while (start.before(end) || start.equals(end));
            } catch (ParseException e) {
                e.printStackTrace();
            }
            return timeData;
        }
    }
}
